/*
	Get all category (include sub-category)
*/
var CateVM = function (funcOverview) {

	// Variables for Category page
	var datacontext = new DataContext(),
		list = ko.observableArray([]),
		listnext = ko.observableArray([]),
        memList = ko.observableArray([]),
		isViewBack = ko.observable(false),
		title = ko.observable(getLocalizeText('browse')),
        currentItemId = ko.observable(''),
		// Get root category and its sub-category
		getCategory = function () {
			// [DONE] Remove or add inform message/actions
			list([]);
            memList([]);
			var listTemp = new ko.observableArray([]);
			var entityTemp = new ko.observable();
			showLoadingDialog();
			isLoading = true;
            
			datacontext.category.getMenu({
				results: listTemp,
				key: 'saveFilecategory'+systemLanguage,
                callbackFunc: function () {
                    if (listTemp().length > 0){
                        list(listTemp()[0].object);
                        memList(listTemp()[0].object);
                        $("#chit-code-content").css({ display: 'block' });
                        setTimeout(function applyScroll() {
                            myScroll.refresh();
                            $("#chit-code-content").css({ display: 'none' });
                        }, 200);
                    }
                    stopLoading();
                }
			}).done(function () {
				CheckExpireSession(
					listTemp, 
					true,
					function() {
						getCategory();
					}, 
					function(entityResult) {
						list(entityResult);
                        memList(entityResult);
                        $("#chit-code-content").css({ display: 'block' });
						setTimeout(function applyScroll() {
							myScroll.refresh();
                            $("#chit-code-content").css({ display: 'none' });
						}, 200);
					});
			}).fail(function (e) {
                    CheckExpireSessionErrorCallback(e,
                                                    function(){
                                                    /* session expired */
                                                    getCategory();
                                                    },
                                                    function(){
                                                    /* not expire */
                                                    });
			});
			
			title(getLocalizeText('browse'));
			isViewBack(false);
		},
		// Browse page
		viewCate = function () {
			if (isJqmGhostClick()) {
				return;
			}
			model.startpageVM.isStartPageWorkflow(0);
			listnext([]);
			_.each(memList(), function (i) {
				listnext.push(i);
			});
			model.isNeedBackButtonAnimation(false);
			model.slidePageAnimation(getLocalizeText('browse'), false, function () {
				if (document.getElementById('browse_category') != null) {
					document.getElementById('browse_category').style.visibility = 'hidden';
				}
				list([]);
				_.each(memList(), function (i) {
					list.push(i);
				});
				isViewBack(false);
				title(getLocalizeText('browse'));
				setTimeout(function applyScroll() {
					myScroll.refresh();
				}, 200);
			});
		},

		/* 
			View page for sub category
			+ item: list view item in category list
		*/
		viewSubCate = function (item) {
			if (isJqmGhostClick()) {
				return;
			}
			listnext([]);
			
			if (!isViewBack()) {
                currentItemId(item.id());
				var cate = _.find(memList(), function (i) {
					return (i.id() === item.id());
				});
				if (cate != undefined) {
					_.each(cate.subCateList(), function (i) {
						listnext.push(i);
					});
				}
			} 

			// [iOS]
			if (isiOS) {
				model.overviewVM.listAnimation([]);
				setTimeout(function () {
					/* add temp overview */
					var strKey = item.id() + '' + currentItemId() + model.settingsVM.getLanguageString();
					if (overviewListFromSubcategory[strKey] != undefined) {
						var listTemp = ko.observableArray(overviewListFromSubcategory[strKey]);
						var listAnimationTemp = ko.observableArray([]);
						var j = 0;
						_.each(listTemp(), function (i) {
							j++;
							if (j <= 8) {
								listAnimationTemp.push(i);
							}
						});
						model.overviewVM.listAnimation(listAnimationTemp());
					}
                   else{
                           model.overviewVM.listAnimation([]);
                   
                   }
				}, 1);
			}
			model.isNeedBackButtonAnimation(true);
			model.slidePageAnimation(item.caption(), true, function () {
				if (document.getElementById('browse_category') != null) {
					document.getElementById('browse_category').style.visibility = 'visible';
				}
				if (viewOverview(item)) {
					stackViews.push(strSubCategory);
					return;
				}
				/* go to sub category */
				stackViews.push(strCategory);
				list([]);
				var cate = _.find(memList(), function (i) {
					return (i.id() === item.id());
				});
				if (cate != undefined) {
					_.each(cate.subCateList(), function (i) {
						list.push(i);
					});
				}
				isViewBack(true);
				title(item.caption());
				setTimeout(function applyScroll() {
					myScroll.refresh();
				}, 200);
			}, false, false, isViewBack() ? true : false, false);
		},

		// Callback to overview list
		viewOverview = function (item) {
			if (!isViewBack())
				return false;
			if (funcOverview !== undefined)
				funcOverview(item);
			return true;
		};
		
	return {
		list: list,
		listnext: listnext,
		getCategory: getCategory,
		viewSubCate: viewSubCate,
		viewCate: viewCate,
		isViewBack: isViewBack,
		title: title,
        currentItemId: currentItemId
	};
};
